package com.bestv.search.common.service.impl;

import com.bestv.search.common.dao.NetInfoHotCommentDao;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.NetInfoHotComment;
import com.bestv.search.common.model.uid.UID;
import com.bestv.search.common.service.NetInfoHotCommentManager;
import org.apache.log4j.Logger;

import java.util.List;

public class NetInfoHotCommentManagerImpl implements NetInfoHotCommentManager {

    private Logger logger = Logger.getLogger(NetInfoHotCommentManagerImpl.class);
    private NetInfoHotCommentDao netInfoHotCommentDao;

    @Override
    public void saveOrUpdateList(DataSourceKey key, List<NetInfoHotComment> list) {
        for (NetInfoHotComment comment : list) {
            try {
                // Save un-exist comment in to DB
                NetInfoHotComment commentInDB = netInfoHotCommentDao.getByCommentsId(comment.getCommentsID(),
                        comment.getSource());
                if (commentInDB == null) {
                    comment.setId(new Long(UID.getUID(comment)));
                    netInfoHotCommentDao.saveOrUpdate(comment);
                }
            } catch (Exception e) {
                // TODO fix this bug in further.
                logger.info("Invalid character found, ignore this comment");
            }
        }
    }

    @Override
    public List<NetInfoHotComment> getNetInfoHotCommentsByNetInfoId(DataSourceKey key, long netInfo_id, int start,
                                                                    int limit) {
        return netInfoHotCommentDao.getNetInfoHotCommentsByNetInfoId(netInfo_id, start, limit);
    }

    @Override
    public int getHotCommentCountByNetInfoId(DataSourceKey key, long netInfo_id) {
        return netInfoHotCommentDao.getHotCommentCountByNetInfoId(netInfo_id);
    }

    public NetInfoHotCommentDao getNetInfoHotCommentDao() {
        return netInfoHotCommentDao;
    }

    public void setNetInfoHotCommentDao(NetInfoHotCommentDao netInfoHotCommentDao) {
        this.netInfoHotCommentDao = netInfoHotCommentDao;
    }
}